package com.eduhzy.ttw.teacher.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.SPUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.EventBusTags;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.entity.ClassDetailsData;
import com.eduhzy.ttw.commonsdk.entity.ClassTeacherData;
import com.eduhzy.ttw.commonsdk.holder.AutoBaseViewHolder;
import com.eduhzy.ttw.teacher.R;
import com.eduhzy.ttw.teacher.app.utils.RxUtils;
import com.eduhzy.ttw.teacher.di.component.DaggerClassTeacherComponent;
import com.eduhzy.ttw.teacher.mvp.contract.ClassTeacherContract;
import com.eduhzy.ttw.teacher.mvp.presenter.ClassTeacherPresenter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.zhy.autolayout.AutoRelativeLayout;

import org.simple.eventbus.Subscriber;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;

;

/**
 * 班级详情教师列表页面
 *
 * @author xubo
 */
@Route(path = RouterHub.APP_CLASSTEACHERACTIVITY)
public class ClassTeacherActivity extends BaseActivity<ClassTeacherPresenter> implements ClassTeacherContract.View {
    @BindView(R.id.public_toolbar_iv_right)
    ImageView ivRight;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout mSwipeLayout;
    @BindView(R.id.public_toolbar_right_iv_layout)
    AutoRelativeLayout right;
    @Autowired(name = Constants.ROUTER_DATA)
    ClassDetailsData mineClassData;
    @Inject
    BaseQuickAdapter<ClassTeacherData, AutoBaseViewHolder> mAdapter;
    @Inject
    RecyclerView.LayoutManager layoutManager;

    /**
     * 当前操作用户是不是班主任
     */

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        //如找不到该类,请编译一下项目
        DaggerClassTeacherComponent
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
        return R.layout.activity_class_teacher;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        if (mineClassData.getBzrgh().equals(SPUtils.getInstance().getString(Constants.USERID))) {
            ivRight.setImageResource(R.drawable.icn_shanchu);
            right.setVisibility(View.VISIBLE);
        } else {
            right.setVisibility(View.GONE);
        }
        rvList.setLayoutManager(layoutManager);
        rvList.setAdapter(mAdapter);
        mSwipeLayout.setOnRefreshListener(() -> {
                    right.setSelected(false);
                    mPresenter.getTeacherByClass(mineClassData.getId());
                }
        );

        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            ClassTeacherData data = mAdapter.getData().get(position);
            data.setBzrgh(mineClassData.getBzrgh());
            if (data.getUserId().equals(SPUtils.getInstance().getString(Constants.USERID)) && !data.isDelete()) {
                ARouter.getInstance().build(RouterHub.APP_CHOOSESUBJECTACTIVITY)
                        .withString(Intent.EXTRA_TITLE, Constants.CHOOSE_SUBJECT)
                        .withParcelable(Constants.ROUTER_DATA, data)
                        .navigation();
            }
        });
        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            final ClassTeacherData data = mAdapter.getData().get(position);
            data.setSelect(!view.isSelected());
            mAdapter.notifyDataSetChanged();
            RxUtil.showDeleteDialog(getActivity(), (dialog, index) -> {
                switch (index) {
                    case 0:
                        dialog.dismiss();
                        data.setSelect(false);
                        mAdapter.notifyDataSetChanged();
                        break;
                    case 1:
                        dialog.dismiss();
                        mPresenter.deleteMemberByManager(data.getClassId(), mineClassData.getBzrgh(), data.getUserId(), position);
                        break;
                    default:

                        break;
                }
            });
        });
        mPresenter.getTeacherByClass(mineClassData.getId());
    }

    @Override
    public void showLoading() {
        if (mSwipeLayout != null) {
            mSwipeLayout.setRefreshing(true);
        }
    }

    @Override
    public void hideLoading() {
        if (mSwipeLayout != null) {
            mSwipeLayout.setRefreshing(false);
        }
    }

    @Override
    public void addFooter() {
        if (mAdapter.getFooterLayoutCount() <= 0) {
            View footer = View.inflate(getActivity(), R.layout.rv_list_footer_button, null);
            mAdapter.addFooterView(footer);
            Button button = (Button) footer.findViewById(R.id.bt_ok);
            button.setText("邀请老师");
            button.setOnClickListener(v -> {
                RxUtils.showShareQRMenu(getActivity(), RxUtil.getClassCode(mineClassData.getBaseNo()));
            });
        }
    }


    @Subscriber(tag = RouterHub.APP_CLASSTEACHERACTIVITY)
    public void onEvents(Message msg) {
        switch (msg.what) {
            case EventBusTags.CHOOSE_SUBJECT_SUCCESS:
                mPresenter.getTeacherByClass(mineClassData.getId());

                mAdapter.notifyDataSetChanged();
                break;
            default:
        }
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(getActivity(), QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public Activity getActivity() {
        return this;
    }


    @OnClick(R.id.public_toolbar_right_iv_layout)
    public void onViewClicked() {
        if (mAdapter.getData().isEmpty()) {
            return;
        }
        for (ClassTeacherData data : mAdapter.getData()) {
            data.setDelete(!right.isSelected());
        }
        mAdapter.notifyDataSetChanged();
        right.setSelected(!right.isSelected());
    }
}
